﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ComponentModel;

namespace MessageRouter.Configuration
{
    public sealed class ReaderConfigurationElement : ConfigurationElement
    {
        const string namePropertyName = "name";
        const string typePropertyName = "type";
        const string addressPropertyName = "address";
        const string behaviorPropertyName = "behavior";

        private static readonly ConfigurationProperty nameProperty = new ConfigurationProperty(namePropertyName, typeof(string), null, ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty typeProperty = new ConfigurationProperty(typePropertyName, typeof(Type), null, new TypeNameConverter(), null, ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty addressProperty = new ConfigurationProperty(addressPropertyName, typeof(Uri), null, ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty behaviorProperty = new ConfigurationProperty(behaviorPropertyName, typeof(string), null, ConfigurationPropertyOptions.None);

        private static readonly ConfigurationPropertyCollection properties;

        [ConfigurationProperty(namePropertyName, IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)base[nameProperty]; }
        }

        [ConfigurationProperty(addressPropertyName, IsKey = false, IsRequired = true)]
        public Uri Address
        {
            get { return (Uri)base[addressProperty]; }
        }

        [TypeConverter(typeof(TypeNameConverter))]
        [ConfigurationProperty(typePropertyName, IsKey = false, IsRequired = true)]
        internal Type Type
        {
            get { return (Type)base[typeProperty]; }
        }

        [ConfigurationProperty(behaviorPropertyName, IsKey = false, IsRequired = false)]
        public string Behavior
        {
            get { return (string)base[behaviorProperty]; }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get { return properties; }
        }

        public ReaderConfigurationElement() { }

        static ReaderConfigurationElement()
        {
            properties = new ConfigurationPropertyCollection
            {
                nameProperty,
                typeProperty,
                addressProperty,
                behaviorProperty,
            };
        }
    }
}
